package com.learn.core.controller.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.learn.common.annotation.Log;
import com.learn.common.annotation.UserLoginToken;
import com.learn.common.enums.BusinessType;
import com.learn.common.enums.OperatorType;
import com.learn.common.enums.ResultEnum;
import com.learn.common.utils.JwtUtil;
import com.learn.common.utils.PageResult;
import com.learn.common.utils.ResultUtil;
import com.learn.common.utils.StringUtils;
import com.learn.common.utils.result.CommonResult;
import com.learn.core.domain.*;
import com.learn.core.dto.LExamineDTO;
import com.learn.core.service.ILExamineQuestionService;
import com.learn.core.service.ILExamineService;
import com.learn.core.service.ILOrganizationService;
import com.learn.framework.web.controller.WebController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Description
 *
 * @author zhaohongyu
 * @ClassName LExamineController
 * @date 2021.04.26 10:22
 * @describe 问卷调查
 */
@Api("问卷调查")
@RestController
@RequestMapping("/manager/examine")
public class LExamineController extends WebController<LExamine> {

    @Autowired
    private ILExamineService ilExamineService;
    @Autowired
    private ILExamineQuestionService ilExamineQuestionService;
    @Autowired
    private ILOrganizationService ilOrganizationService;

    //获取问卷调查列表
    @ApiOperation(value = "获取问卷调查列表")
    @UserLoginToken
    @PostMapping("/list")
    public PageResult<LExamine> list(@RequestBody LExamineDTO lExamineDTO){
        try {
            if (lExamineDTO==null||lExamineDTO.getPage()==null||lExamineDTO.getRows()==null){
                return new PageResult<>(null, null, ResultEnum.ERROR);
            }
            PageHelper.startPage(lExamineDTO.getPage(),lExamineDTO.getRows());
            QueryWrapper<LExamine> queryWrapper = new QueryWrapper<LExamine>();
            if(StringUtils.isNotEmpty(lExamineDTO.getName()))
                queryWrapper.like("examine_name",lExamineDTO.getName());
            if(StringUtils.isNotEmpty(lExamineDTO.getState()))
                queryWrapper.eq("examine_state",lExamineDTO.getState());
            else
                queryWrapper.ne("examine_state","2");
            if(StringUtils.isNotEmpty(lExamineDTO.getOrganizationId()))
                queryWrapper.like("organization_id",lExamineDTO.getOrganizationId());
            Page<LExamine> lExamines = (Page<LExamine>) ilExamineService.list(queryWrapper);
            if (CollectionUtils.isEmpty(lExamines)) {
                return new PageResult<>(null, null, ResultEnum.NOT_EEIST);
            }
            for (LExamine lExamine : lExamines) {
                LOrganization lOrganization = ilOrganizationService.getById(lExamine.getOrganizationId());
                if(null != lOrganization)
                    lExamine.setOrganizationName(lOrganization.getOrganizationName());
            }
            // 返回
            return new PageResult(lExamines.getTotal(), lExamines, ResultEnum.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new PageResult(null,null,ResultEnum.ERROR);
        }
    }

    //获取问卷调查题目列表
    @ApiOperation(value = "获取问卷调查题目列表")
    @UserLoginToken
    @PostMapping("/listQuestion")
    public PageResult<LExamineQuestion> listQuestion(@RequestBody LExamineDTO lExamineDTO){
        try {
            if (lExamineDTO==null||lExamineDTO.getPage()==null||lExamineDTO.getRows()==null){
                return new PageResult<>(null, null, ResultEnum.ERROR);
            }
            PageHelper.startPage(lExamineDTO.getPage(),lExamineDTO.getRows());
            QueryWrapper<LExamineQuestion> queryWrapper = new QueryWrapper<LExamineQuestion>();
            if(StringUtils.isNotEmpty(lExamineDTO.getExamineId()))
                queryWrapper.eq("examine_id",lExamineDTO.getExamineId());
            Page<LExamineQuestion> lExamines = (Page<LExamineQuestion>) ilExamineQuestionService.list(queryWrapper);
            if (CollectionUtils.isEmpty(lExamines)) {
                return new PageResult<>(null, null, ResultEnum.NOT_EEIST);
            }
            // 返回
            return new PageResult(lExamines.getTotal(), lExamines, ResultEnum.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new PageResult(null,null,ResultEnum.ERROR);
        }
    }

    //删除问卷调查
    @ApiOperation(value = "删除问卷调查")
    @PostMapping("/del")
    @UserLoginToken
    @Log(title = "删除问卷调查", businessType = BusinessType.DELETE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> del(@RequestBody LExamine lExamine){
        try {
            if(lExamine!=null){
                if(StringUtils.isNotEmpty(lExamine.getExamineId())){
                    if(ilExamineService.removeById(lExamine.getExamineId()))
                        return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS);
                }
            }
        }catch (Exception e)
        {
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

    //添加问卷调查
    @ApiOperation(value = "添加问卷调查")
    @PostMapping("/add")
    @UserLoginToken
    @Log(title = "添加问卷调查", businessType = BusinessType.INSERT, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> add(@RequestBody LExamine lExamine, HttpServletRequest request){
        String  token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        try {
            if(lExamine!=null) {
                if (StringUtils.isEmpty(lExamine.getExamineName())) {
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_EXAMINE_NAME);
                }
                if (StringUtils.isEmpty(lExamine.getExamineSortId())) {
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_EXAMINE_SORT_ID);
                }
                if (StringUtils.isEmpty(lExamine.getExamineState())) {
                    lExamine.setExamineState("0");
                }
                lExamine.setCreateTime(new Date());
                lExamine.setCreateUser(userId);
                if (ilExamineService.save(lExamine)) {
                    List<LExamineQuestion> lExamineQuestions = lExamine.getlExamineQuestions();
                    for (LExamineQuestion lExamineQuestion: lExamineQuestions) {
                        lExamineQuestion.setExamineId(lExamine.getExamineId());
                    }
                    ilExamineQuestionService.saveBatch(lExamineQuestions);
                    return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS,lExamine.getExamineId());
                }
            }
        }catch (Exception e){
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return  (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

    //更新问卷调查
    @ApiOperation(value = "更新问卷调查")
    @PostMapping("/edit")
    @UserLoginToken
    @Log(title = "更新问卷调查", businessType = BusinessType.UPDATE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> edit(@RequestBody LExamine lExamine, HttpServletRequest request) {
        String  token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        if(lExamine!=null){
            lExamine.setUpdateTime(new Date());
            lExamine.setUpdateUser(userId);
            if(ilExamineService.updateById(lExamine)){
                return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS);
            }
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

    //删除问卷调查题目
    @ApiOperation(value = "删除问卷调查题目")
    @PostMapping("/delQuestion")
    @UserLoginToken
    @Log(title = "删除问卷调查题目", businessType = BusinessType.DELETE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> delQuestion(@RequestBody LExamineQuestion lExamineQuestion){
        try {
            if(lExamineQuestion!=null){
                if(StringUtils.isNotEmpty(lExamineQuestion.getExamineQuestionId())){
                    if(ilExamineQuestionService.removeById(lExamineQuestion.getExamineQuestionId()))
                        return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS);
                }
            }
        }catch (Exception e)
        {
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

    //更新问卷调查题目
    @ApiOperation(value = "更新问卷调查题目")
    @PostMapping("/editQuestion")
    @UserLoginToken
    @Log(title = "更新问卷调查题目", businessType = BusinessType.UPDATE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> editQuestion(@RequestBody LExamineQuestion lExamineQuestion) {
        if(lExamineQuestion!=null){
            if(ilExamineQuestionService.updateById(lExamineQuestion)){
                return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS);
            }
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

    //添加问卷调查题目
    @ApiOperation(value = "添加问卷调查题目")
    @PostMapping("/addQuestion")
    @UserLoginToken
    @Log(title = "添加问卷调查题目", businessType = BusinessType.INSERT, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> addQuestion(@RequestBody LExamineQuestion lExamineQuestion){
        try {
            if(lExamineQuestion!=null) {
                if (StringUtils.isEmpty(lExamineQuestion.getExamineId())) {
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_EXAMINE_ID);
                }
                if (ilExamineQuestionService.save(lExamineQuestion))
                    return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS,lExamineQuestion.getExamineQuestionId());
            }
        }catch (Exception e){
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return  (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

    //发布/归档问卷调查
    @ApiOperation(value = "发布/归档问卷调查")
    @GetMapping("/release")
    @UserLoginToken
    @Log(title = "发布/归档问卷调查", businessType = BusinessType.UPDATE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> release(@RequestParam("id")Integer id,@RequestParam(value = "examineState",required = false)String examineState,
                                        HttpServletRequest request) {
        String  token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        if (StringUtils.isEmpty(id)) {
            return (CommonResult) ResultUtil.error(ResultEnum.LOAD_ERROR);
        }
        LExamine lExamine = new LExamine();
        lExamine.setExamineId(id);
        lExamine.setExamineState(examineState);
        lExamine.setUpdateTime(new Date());
        lExamine.setUpdateUser(userId);
        if(ilExamineService.updateById(lExamine)){
            return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS);
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }
}
